/*
Date:20210503 19:19PM
key:跟TME的题目非常像，但是优化了递归的条件
*/

class Solution {
public:
    void walk(vector<vector<char>>& grid,int i,int j)
    {
        if(grid[i][j]=='1')
        {
        grid[i][j]='0';
        //这里只要保证去往的方向没问题即可
        if(i-1>=0){walk(grid,i-1,j);}
        if(i+1<grid.size()){walk(grid,i+1,j);}
        if(j-1>=0){walk(grid,i,j-1);}
        if(j+1<grid[0].size()){walk(grid,i,j+1);}
        }

    }
    int numIslands(vector<vector<char>>& grid) 
    {
        int re=0;
        for(int i=0;i<grid.size();i++)
        {
            for(int j=0;j<grid[0].size();j++)
            {
                if(grid[i][j]=='1')
                {
                    walk(grid,i,j);
                    re++;
                }
            }
        }
        return re;
    }
};